package service.impl;

import model.DatabaseFrom;
import model.DatabaseFromColumn;
import model.DatabaseJsonModel;
import model.EqualsMessage;

import java.util.*;

/**
 * @author： 杨浩然
 * @date： 2023-02-01 15:35
 */
public class DataHandleServiceImpl  {

    /**
     * 数据处理
     * @param djmList  数据库类型  DATEBASE_TYPE
     * @return
     */
    public List<DatabaseJsonModel> findDataBaseFroms(List<DatabaseJsonModel> djmList) {
        //循环需要对照的多个数据库链接
        for (DatabaseJsonModel djm : djmList) {
            List<DatabaseFrom> excelDatabaseData = new ArrayList();
            Map<String, DatabaseFrom> dfStandardData = djm.getDfStandardData();//标准表
            Map<String, DatabaseFrom> dfContrastData = djm.getDfContrastData();//对照表

            if (djm.getNum()==0&&dfStandardData!=null){
                djm.getErrmsg().add("此数据库为标准数据库，直接输出表信息作为参考 ");

                for (String keyFromName : dfStandardData.keySet()) {
                    DatabaseFrom databaseFrom = dfStandardData.get(keyFromName);
                    List<DatabaseFromColumn> databaseFromColumnLis=new ArrayList<DatabaseFromColumn>();
                    for (String key:databaseFrom.getColumnMap().keySet()) {
                        databaseFromColumnLis.add(databaseFrom.getColumnMap().get(key));
                    }
                    databaseFrom.setDataColumn(databaseFromColumnLis);
                    excelDatabaseData.add(databaseFrom);
                }
                djm.setDataFrom(excelDatabaseData);//将本数据库表信息输出
                continue;
            }

            if (dfContrastData==null){
                djm.getErrmsg().add("无本数据库表相关信息 ");
                continue;
            }
            if (dfStandardData==null){
                djm.getErrmsg().add("无标准数据库表相关信息，输出本数据库表信息 ");
                for (String keyFromName : dfContrastData.keySet()) {
                    excelDatabaseData.add(dfContrastData.get(keyFromName));
                }
                djm.setDataFrom(excelDatabaseData);//将本数据库表信息输出
                continue;
            }

            Map<String, Object> frombinding = djm.getFrombinding();//对应绑定信息
            //循环需要对比的数据库表，逐个和标准库进行对照
            for (String keyFromName : frombinding.keySet()) {
                DatabaseFrom dfContrastDataFrom = dfContrastData.get(keyFromName);
                if (dfContrastDataFrom == null||dfContrastDataFrom.getColumnMap()==null ||dfContrastDataFrom.getColumnMap().keySet().size()==0) {
                    djm.getErrmsg().add("当前数据库表缺失(或此表无列信息)：" + keyFromName + " ");
                    continue;
                }
                String keyFromNameStandard = (String) frombinding.get(keyFromName);
                DatabaseFrom dfStandardDataFrom = dfStandardData.get(keyFromNameStandard);
                if (dfStandardDataFrom == null||dfStandardDataFrom.getColumnMap()==null||dfStandardDataFrom.getColumnMap().keySet().size()==0) {
                    djm.getErrmsg().add("标准数据库无此表(或此表无列信息)：" + keyFromNameStandard + " ");
                    continue;
                }
//                列数据对比
                List<DatabaseFromColumn> databaseFromColumns = fromCompare(dfStandardDataFrom, dfContrastDataFrom);
                dfContrastDataFrom.setDataColumn(databaseFromColumns);//将处理后列信息，放入表信息里
                excelDatabaseData.add(dfContrastDataFrom);//记录处理后的表信息
            }
            djm.setDataFrom(excelDatabaseData);//将处理后的表信息，放入数据源信息里
        }
        return djmList;

    }

    /**
     * 列数据对比 取columnMap信息
     *
     * @param dfStandard 标准列
     * @param dfContrast 对照列
     * @return
     */
    private List<DatabaseFromColumn> fromCompare(DatabaseFrom dfStandard, DatabaseFrom dfContrast) {
        List<DatabaseFromColumn> excellist = new ArrayList<DatabaseFromColumn>();

        Map<String, DatabaseFromColumn> dfStandardMap = dfStandard.getColumnMap();//标准
        Map<String, DatabaseFromColumn> dfContrastMap = dfContrast.getColumnMap();//对照
        Set<String> dfStandarKey = dfStandardMap.keySet();//记录标准列对照情况
        for (String key : dfContrastMap.keySet()) {
            //对照的列
            DatabaseFromColumn dfc = dfContrastMap.get(key);
            //标准的列
            DatabaseFromColumn dfcStandar = dfStandardMap.get(key);
            if (dfcStandar == null) {
                dfc.setEqualsMessage(EqualsMessage.err("列增多 标准：标准表无此列"));
                excellist.add(dfc);//此列加入
                continue;
            }
            //交给类处理具体对比信息
            dfcStandar.equalsColumnUpdata(dfc);
            //删除标准列对照表相应key
            dfStandarKey.remove(key);
            excellist.add(dfc);
        }
        //判断标准列是否还有未判的列 即为缺失
        if (dfStandarKey.size() > 0) {
            for (String key : dfStandarKey) {
                //取标准列信息赋值，因标准列均是独立的，不会影响其他表或者其他数据源的标准表同名列
                DatabaseFromColumn databaseFromColumn = dfStandardMap.get(key);
                databaseFromColumn.setEqualsMessage(EqualsMessage.err("列缺失 标准：本列缺失，此行信息来自标准库表"));
                excellist.add(databaseFromColumn);
            }
        }


        return excellist;
    }


}
